<?php
namespace App\Library\Common;

/**
 * 分表类
 * @author narnia
 */
class Tables
{
    /**
     * 按书籍ID漫画章节，漫画用户打赏
     * @param int $bookId 书籍id
     */
    public static function getCartoonTables(int $bookId)
    {
        $tables = [
            'chapter' => 'chapter',
            'index' => 0
        ];
        if ($bookId >= 10000) {
            $index = floor($bookId / 10000);
            $tables['chapter'] = 'chapter_' . $index;
        }
        return $tables;
    }
    /**
     * 按书籍ID小说章节，漫画用户打赏
     * @param int $bookId 书籍id
     */
    public static function getNovelTables(int $bookId)
    {
        $tables = [
            'novel_chapter' => 'novel_chapter',
            'index' => 0
        ];
        if ($bookId >= 5000) {
            $index = floor($bookId / 5000);
            $tables['novel_chapter'] = 'novel_chapter_' . $index;
        }
        return $tables;
    }
    /**
     * 按书籍ID漫画评论分表信息
     * @param int $bookId 书籍id
     */
    public static function getCartoonCommentTables(int $bookId)
    {
        $tables = [
            'cartoon_comment_post' => 'cartoon_comment_post',
            'cartoon_chapter_post' => 'cartoon_chapter_post',
            'index' => 0
        ];
        if ($bookId >= 2000) {
            $index = floor($bookId / 2000);
            $tables['cartoon_comment_post'] = 'cartoon_comment_post_' . $index;
            $tables['cartoon_chapter_post'] = 'cartoon_chapter_post_' . $index;
            $tables['index'] = $index;
        }
        return $tables;
    }

    /**
     * 按粉丝创建时间的粉丝用户表
     * @param int $userCreateTime 用户创建时间
     */
    public static function getUserCreateTimeTables($userCreateTime = 0)
    {
        $tables = [];
        if ($userCreateTime > 0) {
            // 获取季度
            $se = ceil(date('m', $userCreateTime) / 3);
            $tableFix = date('Y', $userCreateTime) . '_' . $se;
            $tables = [
                'book_support' => 'book_support_' . $tableFix,
                'user_read' => 'user_read_' . $tableFix,
                'user_read_buy' => 'user_read_buy_' . $tableFix,
                'user_consume' => 'user_consume_' . $tableFix,
                'user_pay_log' => 'user_pay_log_' . $tableFix,
                'user_signin' => 'user_signin_' . $tableFix,
                'user_book_shelf' => 'user_book_shelf_' . $tableFix,
                'user_book_notice' => 'user_book_notice_' . $tableFix,
                'user_book_subscribe' => 'user_book_subscribe_' . $tableFix,
                'user_msg' => 'user_msg_' . $tableFix
            ];
        }
        return $tables;
    }

    /**
     * 按粉丝ID的粉丝分表
     * @param int $userMaxId 用户最大主键 | 粉丝id
     * @param int $time 用户创建时间
     */
    public static function getUserIdTables(int $maxId = 0)
    {
        $tables = [
            'user' => 'user',   // 用户
            'user_info' => 'user_info', // 用户信息
            'index' => 0,
        ];
        // 粉丝id  每 300W个一张表
        if ($maxId > 3000000) {
            $index = floor($maxId / 3000000);
            $tables = [
                'user' => 'user_' . $index,
                'user_info' => 'user_info_' . $index,
                'index' => $index
            ];
        }
        return $tables;
    }

    /**
     * 按礼物记录最大id
     */
    public static function getGiftIdTables(int $maxId) {
        $tables = [
            'user_gift' => 'user_gift', // 消费记录
            'index' => 0,
        ];
        if ($maxId > 3000000) {
            $index = floor($maxId / 3000000);
            $tables['user_gift'] = 'user_gift_' . $index;
            $tables['index'] = $index;
        }
        return $tables;
    }
    /**
     * 按礼物日志记录最大id
     */
    public static function getGiftLogIdTables(int $maxId) {
        $tables = [
            'user_gift_log' => 'user_gift_log', // 消费记录
            'index' => 0,
        ];
        if ($maxId > 3000000) {
            $index = floor($maxId / 3000000);
            $tables['user_gift_log'] = 'user_gift_log_' . $index;
            $tables['index'] = $index;
        }
        return $tables;
    }
    /**
     * 按消费记录最大id
     */
//    public static function getConsumeIdTables(int $maxId) {
//        $tables = [
//            'user_consume' => 'user_consume', // 消费记录
//            'index' => 0,
//        ];
//        if ($maxId > 3000000) {
//            $index = floor($maxId / 3000000);
//            $tables['user_consume'] = 'user_consume_' . $index;
//            $tables['index'] = $index;
//        }
//        return $tables;
//    }
    /**
     * 按订单支付ID的分表
     * @param int $maxId 订单表主键id
     */
    public static function getPayIdTables(int $maxId = 0)
    {
        $tables = [
            'pay' => 'pay', // 充值记录
            'index' => 0,
        ];
        // 订单支付id  每300W个一张表
        if ($maxId > 3000000) {
            $index = floor($maxId / 3000000);
            $tables = [
                'pay' => 'pay_' . $index,
                'index' => $index
            ];
        }
        return $tables;
    }
}